package com.bledanliaar.measure.precentor;

import android.os.Handler;

import com.bledanliaar.measure.biz.BleCommandListener;
import com.bledanliaar.measure.biz.BleData;
import com.bledanliaar.measure.biz.IBleData;
import com.bledanliaar.measure.view.IMeasureView;

import java.util.ArrayList;

/**
 * Created by My on 2017/4/26.
 */

public class Precentor {
    IBleData   mIBleData;
    ArrayList<Integer> resultArray;//蓝牙测量的数据集
    Handler   handler;
//    IMeasureView  measureView;
    public Precentor(Handler  handler) {
        this.handler=handler;
        mIBleData=new BleData();
        resultArray=new  ArrayList<Integer>();
//        this.measureView=measureView;
    }
    public void measureBleData(String  bleAddress,OnBledataMeasureListener onBledataMeasureListener){
        get33Command(bleAddress,onBledataMeasureListener);

    }
    private void  get33Command(final String bleAddrass, final OnBledataMeasureListener onBledataMeasureListener){

        mIBleData.get33Command(bleAddrass, new BleCommandListener() {
            @Override
            public void respone(byte[] respone) {
                resultArray.add(disposeArray(respone));
                get34Command(bleAddrass,onBledataMeasureListener);
            }
            @Override
            public void error() {
                onBledataMeasureListener.error();

            }
        });
    };
    private  void  get34Command(final String bleAddrass, final OnBledataMeasureListener onBledataMeasureListener){
        mIBleData.get34Command(bleAddrass, new BleCommandListener() {

            @Override
            public void respone(byte[] respone) {
                resultArray.add(disposeArray(respone));
                get35Command(bleAddrass,onBledataMeasureListener);

            }
            @Override
            public void error() {
                onBledataMeasureListener.error();

            }
        });
    }
    private void  get35Command(String bleAddrass, final OnBledataMeasureListener onBledataMeasureListener){
        mIBleData.get35Command(bleAddrass, new BleCommandListener() {
            @Override
            public void respone(byte[] respone) {
                resultArray.add(disposeArray(respone));
                onBledataMeasureListener.result(resultArray);
            }
            @Override
            public void error() {
                onBledataMeasureListener.error();
            }
        });
    }
    private int  disposeArray(byte []  pm){
        byte[]  temp=new byte[2];
        System.arraycopy(pm,1,temp,0,2);
        return  bytes2short(temp);
    }
    private  short bytes2short(byte[] b) {
        return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
    }
}
